package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.impl.SysUserServiceImpl;
import com.atguigu.spzx.common.entity.SysUser;
import com.atguigu.spzx.common.result.Result;
import com.atguigu.spzx.manager.vo.sysUser.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author flyduck
 * @since 2023-10-25
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserServiceImpl sysUserServiceImpl;

    @ApiOperation("获取用户的角色数据")
    @GetMapping("/getAssignedRoleListByUserId/{userId}")
    public Result<SysUserAssignRoleQueryRespVo> getAssignedRoleListByUserId(@PathVariable Long userId){
        SysUserAssignRoleQueryRespVo respVo = sysUserServiceImpl.getAssignedRoleListByUserId(userId);
        return Result.ok(respVo);
    }

    @ApiOperation("用户分配角色")
    @PostMapping("/assignRoleForUser")
    public Result assignRoleForUser(@RequestBody SysUserAssignRoleReqVo reqVo){
        sysUserServiceImpl.assignRoleForUser(reqVo);
        return Result.ok();
    }

    @ApiOperation("更改用户状态")
    @GetMapping("/updateStatusById/{id}/{status}")
    public Result updateStatusById(@PathVariable Long id,
                               @PathVariable Integer status){
        sysUserServiceImpl.updateStatusById(id,status);
        return Result.ok();
    }


    @ApiOperation("条件分页查询")
    @GetMapping("/getUserPageByQuery/{page}/{limit}")
    public Result<IPage<SysUserQueryRespVo>> getUserPageByQuery(@PathVariable("page") Long page,
                                                                @PathVariable("limit") Long limit,
                                                                SysUserQueryReqVo sysUserQueryReqVo){
        Page<SysUser> pageParam = new Page<>(page, limit);
        Page<SysUserQueryRespVo> pageModel = sysUserServiceImpl.getUserPageByQuery(pageParam, sysUserQueryReqVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("添加用户")
    @PostMapping("/saveUser")
    public Result<Void> saveUser(@RequestBody SysUserSaveReqVo sysUserSaveReqVo){
        sysUserServiceImpl.saveUser(sysUserSaveReqVo);
        return Result.ok();
    }

    @ApiOperation("根据id查询")
    @PostMapping("/getUserById/{id}")
    public Result<SysUserQueryRespVo> getUserById(@PathVariable Long id){
        SysUserQueryRespVo sysUserQueryRespVo = sysUserServiceImpl.getUserById(id);
        return Result.ok(sysUserQueryRespVo);
    }

    @ApiOperation("修改用户")
    @PostMapping("/updateUserById")
    public Result<Void> updateUserById(@RequestBody SysUserUpdateReqVo sysUserUpdateReqVo){
        sysUserServiceImpl.updateUserById(sysUserUpdateReqVo);
        return Result.ok();
    }

    @ApiOperation("根据id逻辑删除用户")
    @DeleteMapping("/deleteUserById/{id}")
    public Result<Void> deleteUserById(@PathVariable Long id){
        sysUserServiceImpl.deleteUserById(id);
        return Result.ok();
    }

}

